package com.example.boot.config;

import com.example.boot.servlet.MyFilter;
import com.example.boot.servlet.MyServletContentListener;
import com.example.boot.servlet.Myservlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;


/**
 * @Configuration的proxyBeanMethods值必须为true，
 * 因为这里MyFilter myfilter=new MyFilter();避免创建多个
 * @author shkstart
 * @creat 2021-04-05-下午9:38
 */
@Configuration
public class MyRegistConfig {
    @Bean
    public ServletRegistrationBean myservlet(){
        Myservlet myservlet=new Myservlet();
        //传入一个Servlet，并且指定拦截路径
        return new ServletRegistrationBean(myservlet,"/my","/my02");
    }
    @Bean
    public FilterRegistrationBean myfilter(){
        MyFilter myfilter=new MyFilter();
        //传入一个Filter，并且指定ServletRegistrationBean,
        // ServletRegistrationBean拦截什么路径，filter也一样
        //下面为两种方法
//        return new FilterRegistrationBean(myfilter, myservlet());
        FilterRegistrationBean<MyFilter> myFilterFilterRegistrationBean = new FilterRegistrationBean<>(myfilter);
        myFilterFilterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*"));
        return myFilterFilterRegistrationBean;
    }
    @Bean
    public ServletListenerRegistrationBean mylistener(){
        MyServletContentListener myServletContentListener = new MyServletContentListener();
        return new ServletListenerRegistrationBean(myServletContentListener);


    }
}
